// 数据库操作
const mongoose = require('mongoose');
// 对象规则验证
const Joi = require('joi');
// 模型规则类
const Schema = mongoose.Schema;

// 订单规则模型
const ordersModel = new Schema({
		// 订单号
		order_number: {
			type: String,
			required: true,
		},
		// 订单价格
		order_price: {
			type: Number,
			default: 0
		},
		// 订单信息
		orders: {
			type: Object,
		},
		// 是否付款
		is_pay: {
			type: Boolean,
			default: false
		},
		// 发件地址
		send_address: [{
			type: String,
			required:true
		}],
		// 收件地址
		receiving_address: [{
			type: String,
			required:true
		}],
		// 是否发货
		is_send: {
			type: Boolean,
			default: false
		},
		// 订单所属用户id
		order_belong: {
			type:mongoose.Schema.Types.ObjectId,
			ref: 'User'
		},
		// 订单所属商品id
		order_goods: {
			type:mongoose.Schema.Types.ObjectId,
			ref: 'Goods'
		},
		// 订单所属店铺id
		order_store: {
			type:mongoose.Schema.Types.ObjectId,
			ref: 'Store'
		},
		// 联系人
		contact: {
			type: String,
			default: ''
		},
		// 订单状态 0 待付款，1 待发货 2 待收货 3 待评价 4已评价
		state: {
			type: Number,
			default: 0
		},
		// 是否评价
		is_evaluate: {
			type: Boolean,
			default: false
		},
		// 评价
		estimate: [{
			type:mongoose.Schema.Types.ObjectId,
			ref: 'Comment'
		}],
		// 用户联系电话
		mobile: {
			type: String,
			required: true,
			minlength: 11,
			maxlength: 11
		},
		// 备注
		remark: {
			type:String,
			default: ''
		},
		// 下单时间
		order_time: {
			type: Date,
			default: Date.now
		}
}, {
	versionKey: false,
	 // timestamps: { createdAt: 'order_time' }
});

// 创建商品集合 
const Orders = mongoose.model('orders',ordersModel,'orders');

// 导出模块成员
module.exports = {
	Orders
}
